package com.wnx.job.common.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.wnx.job.common.api.CommonResult;
import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.common.utils.OssUtil;
import com.wnx.job.common.utils.UploadUtils;
import com.wnx.job.domain.OssCallbackResult;
import com.wnx.job.domain.OssPolicyResult;
import com.wnx.job.modules.jms.dto.StudentParam;
import com.wnx.job.modules.jms.service.JmsStudentService;
import com.wnx.job.modules.nms.model.NmsArticleAttachment;
import com.wnx.job.common.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author by wangnaixing
 * @Description  Oss相关操作接口
 * @Date 2021/12/17 10:16
 */
@Slf4j
@RestController
@Api(tags = "OssController", description = "Oss管理")
@RequestMapping("/aliyun/oss")
public class OssController {
    @Resource
    private OssService ossService;
    @Resource
    private JmsStudentService studentService;

    @ApiOperation(value = "oss上传签名生成")
    @RequestMapping(value = "/policy", method = RequestMethod.GET)
    public CommonResult<OssPolicyResult> policy() {
        OssPolicyResult result = ossService.policy();
        return CommonResult.success(result);
    }

    @ApiOperation(value = "oss上传成功回调")
    @RequestMapping(value = "/callback", method = RequestMethod.POST)
    public CommonResult<OssCallbackResult> callback(HttpServletRequest request) {
        OssCallbackResult ossCallbackResult = ossService.callback(request);
        return CommonResult.success(ossCallbackResult);
    }


    @PostMapping("/attachment")
    @ApiOperation("处理咨询附件文件上传")
    public ResultData<NmsArticleAttachment> uploadAttachmentFile(MultipartFile uploadAttachmentFile) throws IOException {
        log.info("==========正在处理处理咨询附件文件上传============");
        //文件名唯一
        String filename = UploadUtils.getUUIDName(uploadAttachmentFile.getOriginalFilename());
        String attachmentUrl = OssUtil.uploadByOther(filename, uploadAttachmentFile.getInputStream());
        log.info("附件上传成功上传到阿里云对象存储，附件上传之后的访问地址是:{}", attachmentUrl);
        //构造附件对象
        NmsArticleAttachment articleAttachment = new NmsArticleAttachment();
        articleAttachment.setName(filename);
        articleAttachment.setUrl(attachmentUrl);
        return ResultData.success(Const.UPLOAD_FILE_SUCCESS, articleAttachment);
    }


    @PostMapping("/article")
    @ApiOperation("上传咨询配图到阿里云OSS存储中")
    public ResultData<String> uploadArticleImagesByOss(MultipartFile uploadImage) throws IOException {
        log.info("==========正在处理上传咨询配图到阿里云OSS存储中============");
        String fileName = UploadUtils.getUUIDName(Objects.requireNonNull(uploadImage.getOriginalFilename()));
        String imgUrl = OssUtil.upload(fileName, uploadImage.getInputStream());
        log.info("咨询配图成功上传到阿里云对象存储，图片上传之后的访问地址是:{}", imgUrl);
        return ResultData.success(Const.UPLOAD_FILE_SUCCESS, imgUrl);
    }

    /**
     * 学生上传一寸证件照
     *
     * @param uploadImage
     * @return
     */
    @PostMapping("/student")
    @ApiOperation("学生上传一寸证件照")
    public ResultData<String> uploadStudentImage(MultipartFile uploadImage) throws IOException {
        log.info("==========正在处理上传一寸证件照到阿里云OSS存储中============");
        String filename = UploadUtils.getUUIDName(uploadImage.getOriginalFilename());
        String imgUrl = OssUtil.upload(filename, uploadImage.getInputStream());
        log.info("一寸照成功上传到阿里云对象存储，图片上传之后的访问地址是:{}", imgUrl);
        return ResultData.success(Const.UPLOAD_FILE_SUCCESS, imgUrl);
    }


    /**
     * 通过Excel文件批量添加学生信息
     *
     * @param excelFile
     * @return
     */
    @PostMapping("/saveStudentByBatch")
    @ApiOperation("通过Excel文件批量添加学生信息")
    public ResultData<String> saveStudentByBatch(@RequestPart("excelFile") MultipartFile excelFile) {
        log.info("【系统正在到批量上传学生信息中.....】");
        try {
            ImportParams params = new ImportParams();
            //表标题的行数
            params.setTitleRows(0);
            //表头行数
            params.setHeadRows(1);
            List<StudentParam> studentParamList = ExcelImportUtil.importExcel(excelFile.getInputStream(), StudentParam.class, params);

            //调用服务，批量保存学生信息。
            studentService.addBatch(studentParamList);
            log.info("【======批量添加学生信息成功了！！=====】");
            return ResultData.success(Const.ADD_STUDENT_FAIL);

        } catch (Exception e) {
            e.printStackTrace();
            Asserts.fail("批量添加学生信息失败！");
        }
        return ResultData.success(Const.ADD_STUDENT_FAIL);
    }





}
